package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacStaff;
import com.csun.cmny.provider.model.domain.UacStaffStatus;
import com.csun.cmny.provider.model.dto.staff.*;
import com.csun.cmny.provider.model.dto.volunteer.VolunteerModifyPwdDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.RedisService;
import com.csun.cmny.provider.service.UacStaffService;
import com.csun.cmny.provider.service.UacStaffStatusService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/staff", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacStaffController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacStaffService uacStaffService;

    @Resource
    private UacStaffStatusService uacStaffStatusService;

    /**
     * 服务人员添加
     *
     * @param staffDto
     * @param br binding result
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/add")
    public Wrapper addStaff(@RequestBody StaffDto staffDto, BindingResult br) {

        Integer result = uacStaffService.addStaff(staffDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 服务人员修改
     *
     * @param staffDto
     * @param br
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/edit")
    public Wrapper editVolunteer(@RequestBody StaffDto staffDto,
                                 BindingResult br) {

        Integer result = uacStaffService.updateStaff(staffDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 服务人员删除
     *
     * @param staffId
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/deleteById/{staffId}")
    public Wrapper deleteStaffById(@PathVariable Long staffId) {

        // 校验志愿者Id是否为空
        Preconditions.checkArgument(staffId != null, cmnyMessageSource
                .getMessage("volunteer.id.null", null));

        Integer result = uacStaffService.deleteStaffById(staffId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 分页查询
     *
     * @param staffQueryDto
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/listPage")
    public Wrapper listPage(@RequestBody StaffQueryDto staffQueryDto) {

        staffQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacStaffService.listPage(staffQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 修改服务人员密码
     *
     *
     * @param modifyPwdDto
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/modifyPwd")
    public Wrapper modifyPwd(@RequestBody ModifyPwdDto modifyPwdDto) {

        Integer result = uacStaffService.modifyPwd(modifyPwdDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 服务人员状态切换
     *
     * @param status
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/changeStaffStatus")
    public Wrapper changeStaffStatus(@RequestParam Integer status){

        Integer result = uacStaffStatusService.changeStatus(getLoginAuthDto(),status);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 服务人员状态切换日志查询
     *
     * @param
     *
     * @return  the wrapper.
     */
    @GetMapping (value = "/getStatus")
    public Wrapper getStatus(){

        //查询当前状态
        UacStaff uacStaff = uacStaffService.selectByUserId(getLoginAuthDto().getUserId());
        if (PublicUtil.isEmpty(uacStaff)) {
            throw new UacBizException(ErrorCodeEnum.UAC10011047);
        }

        Integer result=uacStaff.getStatus();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 获取服务人员状态
     *
     * @param
     *
     * @return the wrapper.
     */
    @GetMapping (value = "/getStatusTest")
    public Wrapper getStatusTest(){
        //根据id查询当前登录的服务人员信息
        UacStaff uacStaffTest = uacStaffService.selectByUserIdTest(getLoginAuthDto().getUserId());
        if (PublicUtil.isEmpty(uacStaffTest)) {
            throw new UacBizException(ErrorCodeEnum.UAC10011047);
        }

        //获取服务人员状态
        Integer result=uacStaffTest.getStatus();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 服务人员状态切换日志查询
     *
     * @param
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @PostMapping(value = "/getStatusLog")
    public Wrapper getStatusLog(@RequestBody @Valid StaffStatusQueryDto staffStatusQueryDto, BindingResult br){

        PageInfo pageInfo=uacStaffStatusService.listPage(staffStatusQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 查询所有不是专家的服务人员
     *
     *
     *
     * @return  the wrapper.
     */
    @PostMapping(value = "/selectStaffByNotSpecoalist")
    public Wrapper selectStaffByNotSpecoalist(@RequestBody StaffQueryDto staffQueryDto){

        staffQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacStaffService.selectStaffByNotSpecoalist(staffQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    //服务人员查询自己的服务人员id
    @PostMapping(value = "/selectStaffIdByToken")
    public Wrapper selectStaffIdByToken() {

        UacStaff uacStaff = uacStaffService.selectStaffByUserId(getLoginAuthDto().getUserId());


        if (PublicUtil.isEmpty(uacStaff)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("staff.is.not.staff", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacStaff);
    }


}